var editQuestionnaireTmpl = '问卷标题：<input type="text" class="qtextbox" name="qnName" id="qnName" value="#questionnaireName#" />&nbsp;&nbsp;'+
			'问卷分类：<select name="qnGroup" class="qselect" id="qnGroup">#questionnaireGroupList#</select><br/>'+
			'问卷描述：<input type="text" class="qtextbox" name="qnDescription" id="qnDescription" value="#questionnaireDescription#" />&nbsp;&nbsp;'+
			'问卷类型：<select class="qselect" name="qnType" id="qnType">'+
			'<option value="1"#questionnaireType1#>标准</option><option value="2"#questionnaireType2#>PK台</option>'+
			'</select><br/><input type="button" class="qbutton" name="button" onClick="submitQuestionnaire()" value="#buttonValue#">';
			
var editQuestionTmpl = '问题题目：<input type="text" class="qtextbox" id="qTitle" value="#questionTitle#" />&nbsp;&nbsp;问题类型：<select class="qselect" id="qType">'+
			'<option value="1" #questionType1#>单选</option>'+
			'<option value="2" #questionType2#>多选</option>'+
			'</select><ul id="option_edit">#optionList#</ul><input type="button" class="qbutton" onClick="addOption()" value="添加选项"/>&nbsp;'+
			'<input type="button" class="qbutton" name="button" onClick="submitQuestion()" value="#buttonValue#">&nbsp;'+
			'<input type="button" class="qbutton" name="button" onClick="cancelQuestion()" value="取消">';

var editQuestionPKTmpl = '<label for="qTrue">正方：</label><input type="text" class="qtextbox" id="qTrue" value="" /><br/>'+
			'<label for="qFalse">反方：</label><input type="text" class="qtextbox" id="qFalse" value="" /><br/>'+
			'<input type="button" class="qbutton" name="button" onClick="submitQuestion()" value="确认并显示">';
			
var editOptionTmpl = '<li>选项#No#：<input type="text" class="qtextbox" name="edit_option" value="#value#"/></li>';

var displayQuestionnaireTmpl = '<div id="dqnHead"><h2>#questionnaireName#</h2><h3>#questionnaireDescription#</h3></div>'+
								'<div id="dqList">#questionList#</div>';

var displayQuestionTmpl = '<div class="dqNode"><div class="dqTitle">#questionTitle#</div><div class="doList">#optionList#</div>'+
						'<input type="button" class="qbutton" onClick="deleteQuestion(#questionIndex#)" value="删除">&nbsp;#ctrl#</div>';

var displayQuestionPKTmpl = '<div class="dqNodePK"><div class="dqTrue"><h3>正方</h3><div>#dqTrue#</div>	<input type="button" class="qbutton" value="支持正方"></div>'+
			'<div class="dqFalse"><h3>反方</h3><div>#dqFalse#</div><input type="button" class="qbutton" value="支持反方"></div><div class="none"></div></div>';

var displayOptionTmpl = '<div class="#optionClass#">#No#：<input type="#optionType#" name="#optionName#">#optionText#</div>';

var groupList = new Array();
var groupListIsShow = false;
getGroupList();

var questionnaire = getNewQuestionnaire();
var editing_question_id;
var editing_question;
var isNewQuestion = true;

function editQuestionnaire(i){
	var inner = new String('');
	inner = editQuestionnaireTmpl;
	inner = inner.replace('#questionnaireName#', questionnaire.questionnaireName);
	inner = inner.replace('#questionnaireDescription#', questionnaire.questionnaireDescription);
	inner = inner.replace('#buttonValue#', (i == 1 ? '创建' : '修改'));
	var groupStr = '';
	for(var i = 0; i < groupList.length; i++){
		if(i == questionnaire.questionnaireGroup){
			groupStr += '<option value="'+i+'" selected="selected">'+groupList[i]+'</option>';
		}else{
			groupStr += '<option value="'+i+'">'+groupList[i]+'</option>';
		}
	}
	inner = inner.replace('#questionnaireGroupList#', groupStr);
	groupListIsShow = true;
	inner = inner.replace('#questionnaireType'+questionnaire.questionnaireType+'#', ' selected="selected"');
	inner = inner.replace(/#questionnaireType\d+#/, '');
	
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
	return;
}

function addQuestion(i){
	if(i == -1){
		editing_question_id = questionnaire.questionList.length;
		editing_question = getNewQuestion();
		editing_question.optionList[editing_question.optionList.length] = getNewOption();
		isNewQuestion = true;
	}
	else{
		editing_question = questionnaire.questionList[i];
		editing_question_id = i;
		isNewQuestion = false;
	}
	display_edit_question();
	return;
}

function cancelQuestion(){
	var inner = new String('');
	inner = '<input type="button" class="qbutton" value="添加问题" onClick="addQuestion(-1)"/>';
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
}

function addOption(){
	cacheOption();
	editing_question.optionList[editing_question.optionList.length] = getNewOption();
	display_edit_question();
	return;
}

function submitQuestionnaire(){
	questionnaire.questionnaireName = document.getElementById('qnName').value;
	questionnaire.questionnaireDescription = document.getElementById('qnDescription').value;
	questionnaire.questionnaireGroup = document.getElementById('qnGroup').value;
	questionnaire.questionnaireType = document.getElementById('qnType').value;
	display_questionnaire();
	addQuestion(-1)
	return;
}

function submitQuestion(){
	if(questionnaire.questionnaireType == 1){
		cacheOption();
		if(editing_question.questionTitle.trim() == ''){
			alert('请填写问题题目');
			return;
		}
		if(editing_question.optionList.length == 0 || (editing_question.optionList.length == 1 && editing_question.optionList[0].optionText.trim() == '')){
			alert('请先添加选项！');
			return;
		}
		questionnaire.questionList[editing_question_id] = editing_question;
		var inner = new String('');
		inner = '<input type="button" class="qbutton" value="添加问题" onClick="addQuestion(-1)"/>';
		document.getElementById('questionnaire_edit_panel').innerHTML = inner;
	}
	else if(questionnaire.questionnaireType == 2){
		var z = new String(document.getElementById('qTrue').value); 
		if(z.trim() == ''){
			alert('请填写正方观点～');
			return;
		}
		var f = new String(document.getElementById('qFalse').value);
		if(f.trim() == ''){
			alert('请填写反方观点～');
			return;
		}
		var q = getNewQuestion();
		q.optionList[0] = getNewOption();
		q.optionList[0].optionText = z;
		q.optionList[1] = getNewOption();
		q.optionList[1].optionText = f;
		questionnaire.questionList[0] = q;
	}
	display_questionnaire();
	return;
}

function cacheOption(){
	editing_question.questionTitle = document.getElementById('qTitle').value;
	editing_question.questionType = document.getElementById('qType').value;
	var tlist = document.getElementsByName('edit_option');
	editing_question.optionList.splice(0, editing_question.optionList.length);
	for(var i = 0; i < tlist.length; i++){
		if(tlist[i].value.trim() == ''){
			continue;
		}
		var op = getNewOption();
		op.optionText = tlist[i].value;
		editing_question.optionList[i] = op;
	}
	if(editing_question.optionList.length == 0){
		editing_question.optionList[0] = getNewOption();
	}
	return;
}

function deleteQuestion(index){
	questionnaire.questionList.splice(index,1);
	display_questionnaire();
}

function questionUp(index){
	if(index == 0){
		return;
	}
	var temp = questionnaire.questionList[index-1];
	questionnaire.questionList[index-1] = questionnaire.questionList[index];
	questionnaire.questionList[index] = temp;
	display_questionnaire();
}

function questionDown(index){
	if(index == questionnaire.questionList.length - 1){
		return;
	}
	var temp = questionnaire.questionList[index+1];
	questionnaire.questionList[index+1] = questionnaire.questionList[index];
	questionnaire.questionList[index] = temp;
	display_questionnaire();
}

function display_edit_question(){
	var inner = new String('');
	if(questionnaire.questionnaireType == 1){
		inner = editQuestionTmpl;
		inner = inner.replace('#questionTitle#', editing_question.questionTitle);
		inner = inner.replace('#buttonValue#', (isNewQuestion ? '创建问题' : '修改问题'));
		inner = inner.replace('#questionType'+editing_question.questionType+'#', ' selected="selected"');
		inner = inner.replace(/#questionType\d+#/, '');
		optionList = new String('');
		for(var i = 0; i < editing_question.optionList.length; i++){
			var temp = editOptionTmpl;
			temp = temp.replace('#value#', editing_question.optionList[i].optionText)
			temp = temp.replace('#No#', i+1);
			optionList += temp;
		}
		inner = inner.replace('#optionList#', optionList);
	}
	else if(questionnaire.questionnaireType == 2){
		inner = editQuestionPKTmpl;
	}
	document.getElementById('questionnaire_edit_panel').innerHTML = inner;
}


function display_questionnaire(){
	var inner = new String('');
	inner = displayQuestionnaireTmpl;
	inner = inner.replace('#questionnaireName#', questionnaire.questionnaireName);
	inner = inner.replace('#questionnaireDescription#', questionnaire.questionnaireDescription);
	var qlist = new String('');
	if(questionnaire.questionnaireType == 1){
		for(var i = 0; i < questionnaire.questionList.length; i++){
			var tq = displayQuestionTmpl;
			tq = tq.replace('#questionTitle#', '问题1:'+questionnaire.questionList[i].questionTitle);
			var ctrl = new String('');
			if(i != 0){
				ctrl += '<input type="button" class="qbutton" onClick="questionUp(#questionIndex#)" value="上移">&nbsp;';
			}
			if(i != (questionnaire.questionList.length-1)){
				ctrl += '<input type="button" class="qbutton" onClick="questionDown(#questionIndex#)" value="下移">';
			}
			tq = tq.replace('#ctrl#', ctrl);
			tq = tq.replace(new RegExp('#questionIndex#',"gm"), i);
			var olist = new String('');
			for(var j = 0; j < questionnaire.questionList[i].optionList.length; j++){
				var to = displayOptionTmpl;
				to = to.replace('#No#', i+1);
				to = to.replace('#optionClass#', 'optionClass');
				to = to.replace('#optionType#', (questionnaire.questionList[i].questionType == 1 ? 'radio' : 'checkbox'));
				to = to.replace('#optionName#', 'question'+i);
				to = to.replace('#optionText#', questionnaire.questionList[i].optionList[j].optionText);
				olist += to;
			}
			tq = tq.replace('#optionList#', olist);
			qlist += tq;
		}
		inner = inner.replace('#questionList#', qlist);
	}
	else if(questionnaire.questionnaireType == 2){
		qlist = displayQuestionPKTmpl;
		if(questionnaire.questionList.length != 0){
			qlist = qlist.replace('#dqTrue#', questionnaire.questionList[0].optionList[0].optionText);
			qlist = qlist.replace('#dqFalse#', questionnaire.questionList[0].optionList[1].optionText);
		}
		else{
			qlist = '';
		}
		inner = inner.replace('#questionList#', qlist);
	}
	document.getElementById('questionnaire_display_panel').innerHTML = inner;
	return;
}


function getNewQuestionnaire(){
	var q = new Object();
	q.questionnaireName = new String('');
	q.questionnaireDescription = new String('');
	q.questionnaireType = 1;
	q.questionnaireGroup = 1;
	q.questionList = new Array();
	return q;
}

function getNewQuestion(){
	var q = new Object();
	q.questionTitle = new String('');
	q.questionType = 1;
	q.optionList = new Array();
	return q;
}

function getNewOption(){
	var q = new Object();
	q.optionText = new String('');
	return q;
}

function createQuestionnaire(){
	if(questionnaire.questionnaireName.trim() != '' && questionnaire.questionList.length != 0){
		var s = questionnaireToJSONString(questionnaire);
		submitJSON(s);
	}
	else{
		alert('问卷信息不完整！');
	}
}

function getQuestionnaire(userid){
	getJSON(userid);
}

function questionnaireToJSONString(questionnaire){
	var str = new String('');
	str = '{"questionnaireName":"' + questionnaire.questionnaireName + '","questionnaireDescription":"' + questionnaire.questionnaireDescription +
	'","questionnaireType":' + questionnaire.questionnaireType.toString() + ',"questionnaireGroup":' + questionnaire.questionnaireGroup.toString() +
	',"questionList":[';
	for (var i = 0; i < questionnaire.questionList.length; i++){
		if(i != 0){
			str += ',';
		}
		str += questionToJSONString(questionnaire.questionList[i]);
	}
	str += ']}';
	return str;
}

function questionToJSONString(question){
	var str = new String('');	
	str += '{"questionTitle":"' + question.questionTitle + '","questionType":' + question.questionType.toString() + ',"optionList":[';
	for (var i = 0; i < question.optionList.length; i++){
		if(i != 0){
			str += ',{"optionText":"' + question.optionList[i].optionText + '"}';
		}
		else{
			str += '{"optionText":"' + question.optionList[i].optionText + '"}';
		}
	}
	str += ']}';
	return str;
}
var xmlHttp;
function submitJSON(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	var url="createQuestionnaire";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=submitStateChanged;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(str);
}

function getJSON(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	var url="getQuestionnaire-";
	url=url+str;
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=getStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function getGroupList(){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	var url="getgroup";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=getGroupChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function submitStateChanged() { 
	if(xmlHttp.readyState==4){ 
		location=xmlHttp.responseText;
	}
}

function getStateChanged(){
	if(xmlHttp.readyState==4){
		questionnaire = eval('('+xmlHttp.responseText+')');
		var inner = new String('');
		inner = '<input type="button" value="添加问题" onClick="addQuestion(-1)"/>';
		document.getElementById('questionnaire_edit_panel').innerHTML = inner;
		display_questionnaire();
	}
}
function getGroupChanged(){
	if(xmlHttp.readyState==4){
		groupList = eval('('+xmlHttp.responseText+')');
		if(groupListIsShow){
			var groupStr = '';
			for(var i = 0; i < groupList.length; i++){
				if(i == questionnaire.questionnaireGroup){
					groupStr += '<option value="'+i+'" selected="selected">'+groupList[i]+'</option>';
				}else{
					groupStr += '<option value="'+i+'">'+groupList[i]+'</option>';
				}
			}
			document.getElementById("qnGroup").innerHTML = groupStr;
		}
	}
}